package com.yzr.common.thread;

import java.util.concurrent.CountDownLatch;

/**
 * @description 压榨cpu，出现乱序
 * @author: yx-0173
 * @date: 2021-06-18 17:29
 **/
public class Ordering {

    public static long MAX = Long.MAX_VALUE;
    private static int a, b, x, y;

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < MAX; i++) {
            CountDownLatch latch = new CountDownLatch(2);
            a = 0;
            b = 0;
            x = 0;
            y = 0;
            Thread thread1 = new Thread(() -> {
                a = 1;
                x = b;
                latch.countDown();
            });
            Thread thread2 = new Thread(() -> {
                b = 1;
                y = a;
                latch.countDown();
            });

            thread1.start();
            thread2.start();
            latch.await();
            String result = "第" + i + "次(" + x + "," + y + ")";
            if (x == 0 && y == 0) {
                System.err.println(result);
                break;
            }
        }
    }


}
